package com.sea.app.module.identity.domain.algorithm;

import com.sea.app.common.excrption.BusinessException;
import com.sea.app.module.identity.domain.User;

/**
 * Created by 31417 on 2017/4/23.
 */
public class Login {
    private final User user;
    private Login(User user){
        this.user = user;
    }

    /**
     * 根据数据库查询出的用户创建授权对象
     * @param user
     * @return
     */
    public static Login on(User user){
        return new Login(user);
    }

    /**
     * 登录
     * @param loginPassword 登录所用密码
     */
    public void login(String loginPassword){
        if(user == null){
            throw BusinessException.on("login.userNotfind","用户不存在！");
        }
        if(loginPassword==null){
            throw BusinessException.on("login.loginParamError","登录参数错误！");
        }
        if(!user.isEnable()){
            throw BusinessException.on("login.userNotEnable","用户已禁用！");
        }
        if(!user.passwordEquest(loginPassword)){
            throw BusinessException.on("login.loginNameOrPasswordError","账户或者密码错误！");
        }
    }


}
